package com.park.platform.service.impl;

import com.park.common.bean.ArticleInputView;
import com.park.common.bean.PageBean;
import com.park.common.constant.IDBConstant;
import com.park.common.exception.MessageException;
import com.park.common.poo.ArticleInfo;
import com.park.common.util.DateUtil;
import com.park.common.util.FileUtil;
import com.park.common.util.StrUtil;
import com.park.platform.dao.IBaseDao;
import com.park.platform.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
    文章
 */
@Service
public class ArticleServiceImpl extends BaseService implements IArticleService {

    @Autowired
    private IBaseDao baseDao;

    @Override
    public PageBean getArticles(ArticleInputView articleInputView){
        StringBuilder headSql = new StringBuilder("SELECT *");
        StringBuilder bodySql = new StringBuilder(" FROM article_info");
        StringBuilder whereSql = new StringBuilder(" WHERE articleStatus = ").append(IDBConstant.LOGIC_STATUS_YES);
        whereSql.append(" AND serviceType = :serviceType");
        whereSql.append(" ORDER BY articleSeq DESC, createTime DESC");

        PageBean pageBean = super.getPageBean(headSql, bodySql, whereSql, articleInputView);
        List<Map<String, Object>> list = pageBean.getList();
        for(Map<String, Object> map : list){
            map.put("articlePic", FileUtil.getImgHttp(StrUtil.objToStr(map.get("articlePic"))));
        }

        return pageBean;
    }

    @Override
    public Map<String, Object> getArticleDetails(int articleId){
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("articleInfo", getArticleInfo(articleId));
        return resultMap;
    }

    @Override
    public ArticleInfo getArticleInfo(int articleId){
        ArticleInfo articleInfo = baseDao.getToEvict(ArticleInfo.class, articleId);
        if(IDBConstant.LOGIC_STATUS_YES.equals(articleInfo.getServiceType())) {
            articleInfo.setArticlePic(FileUtil.getImgHttp(articleInfo.getArticlePic()));
        }
        return articleInfo;
    }

    /***************************************************************************************************************************************/
    @Override
    public PageBean getArticlesIN(ArticleInputView articleInputView){
        String articleStatus = articleInputView.getArticleStatus();
        String serviceType = articleInputView.getServiceType();

        StringBuilder headSql = new StringBuilder("SELECT *");
        StringBuilder bodySql = new StringBuilder(" FROM article_info ai");
        StringBuilder whereSql = new StringBuilder(" WHERE 1=1");
        if(StrUtil.isNotBlank(articleStatus)){
            whereSql.append(" AND articleStatus = :articleStatus");
        }
        whereSql.append(" AND serviceType = :serviceType");
        whereSql.append(" ORDER BY ai.createTime DESC");

        PageBean pageBean = super.getPageBean(headSql, bodySql, whereSql, articleInputView);
        List<Map<String, Object>> list = pageBean.getList();
        for(Map<String, Object> map : list){
            map.put("articlePic", FileUtil.getImgHttp(StrUtil.objToStr(map.get("articlePic"))));
        }

        return pageBean;
    }

    @Override
    public Integer saveArticle(ArticleInfo articleInfo, MultipartHttpServletRequest multipartRequest) throws IOException {
        Integer articleId = articleInfo.getArticleId();
        boolean newFlag = articleId == null;
        String nowDate = DateUtil.getNowDate();
        if(StrUtil.isBlank(articleInfo.getArticleBody())) throw new MessageException("请填写文章描述");
        if(articleId == null){ //添加
            articleInfo.setArticlePic(StrUtil.EMPTY);
            articleInfo.setArticleStatus(IDBConstant.LOGIC_STATUS_YES);
            articleInfo.setCreateTime(nowDate);
            baseDao.save(articleInfo, null);
            articleId = articleInfo.getArticleId();
        }else{ //修改
            ArticleInfo articleInfoDB = getArticleInfo(articleId);
            if(articleInfoDB == null) throw new MessageException("信息不存在！");
            articleInfoDB.setArticleTitle(articleInfo.getArticleTitle());
            articleInfoDB.setArticleBody(articleInfo.getArticleBody());
            articleInfoDB.setArticleSeq(articleInfo.getArticleSeq());
            articleInfoDB.setArticleAbstract(articleInfo.getArticleAbstract());
            articleInfoDB.setUpdateTime(nowDate);
            baseDao.save(articleInfoDB, articleInfoDB.getArticleId());
            articleId = articleInfoDB.getArticleId();
        }
        if(IDBConstant.LOGIC_STATUS_YES.equals(articleInfo.getServiceType())){
            List<MultipartFile> multipartFiles = FileUtil.getMultipartFiles(multipartRequest);
            if (multipartFiles.size() > 0) { //保存活动图腾图片
                ArticleInfo articleInfoDB = getArticleInfo(articleId);
                articleInfoDB.setArticlePic(FileUtil.saveFile(multipartFiles.get(0), multipartRequest).toString());
                baseDao.save(articleInfoDB, articleInfoDB.getArticleId());
            } else if (newFlag) { //文章必须上传图片
                throw new MessageException("请上传文章图片");
            }
        }
        return articleId;
    }

    @Override
    public void newDeleteArticle(int articleId){
        ArticleInfo articleInfo = getArticleInfo(articleId);
        if(IDBConstant.LOGIC_STATUS_NO.equals(articleInfo.getArticleStatus())){
            baseDao.delete(articleInfo);
        }else {
            articleInfo.setArticleStatus(IDBConstant.LOGIC_STATUS_NO);
            baseDao.save(articleInfo, articleInfo.getArticleId());
        }
    }

}
